package com.gujia.dao;

import com.gujia.model.Comment;
import com.gujia.model.Question;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

/**
 * Created by GuJia on 2017/6/30.
 */
@Mapper
public interface CommentDao {

    String TABLE_NAME = " comment ";
    String INSERT_FIELDS = " content, user_id, entity_id, entity_type, created_date, status";
    String SELECT_FIELDS = " id, " + INSERT_FIELDS;



    @Insert(value = {"insert into ", TABLE_NAME, "(", INSERT_FIELDS, ") values (#{content},#{userId},#{entityId},#{entityType},#{createdDate},#{status})"})
    int addComment(Comment comment);

    @Select({"select", SELECT_FIELDS," from ",TABLE_NAME," where entity_id =#{entityId} and entity_type=#{entityType} order by created_date desc"})
    List<Comment> selectByEntity(@Param("entityId") int entityId, @Param("entityType") String entityType);

    @Select({"select count(id) from ", TABLE_NAME, " where entity_id=#{entityId} and entity_type=#{entityType} "})
    int getCommentCount(@Param("entityId") int entityId, @Param("entityType") String entityType);

    @Update({"update ",TABLE_NAME," set status=#{status} where id=#{id}"})
    void updateSatus(int id,String status);

    @Select({"select", SELECT_FIELDS," from ",TABLE_NAME," where id =#{id} "})
    Comment getCommentById(@Param("id") int id);

    @Select({"select count(id) from ", TABLE_NAME, " where user_id=#{userId}"})
    int getUserCommentCount(int userId);


}
